package com.movitech.mobile.cz.base.bizconfig.service.impl;

import com.movitech.mobile.cz.base.common.license.LicenseUtil;
import com.movitech.mobile.cz.base.common.sms.SmsUtil;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2018/4/2.
 */
@Service("messageUtilService")
public class MessageUtilService {

    private  Logger logger = LoggerFactory.getLogger(MessageUtilService.class);

    @Autowired
    private  SmsUtil smsUtil;

    //创建固定大小的线程池
    private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);

    /**
     * 异步发送消息接口
     * @param configValue 短信内容
     * @param phone
     * @param params
     */
    public  void messageSendAsyn(String configValue,String phone,String[] params){
        LicenseUtil.INSTANCE.validate();
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
//                String configValue=sCommentConfigService.getValueByCode(configkey);
                if(StringUtils.isEmpty(configValue)||StringUtils.isEmpty(phone)){
                    logger.info(configValue+"configValue为空,请从新配置");
                    return;
                }else {
                    if(params!=null&&params.length>0){
                        String restr=configValue;
                        for(int i=0;i<params.length;i++){
                            if(restr.indexOf("["+i+"]")!=-1){
                                restr=restr.replace("["+i+"]",params[i]);
                            }
                        }
                        logger.info(phone+"=====>发送消息内容:" + restr);
                        System.out.println(phone+"=====>发送消息内容:" + restr);
                        boolean result = smsUtil.sendSms(phone,restr);
                        System.out.println(phone+"=====>发送消息result:" + result);
                        logger.info(phone+"=====>发送消息result:" + result);
                    }
                }
            }
        });
    }

    /**
     * 同步发送消息接口
     * @param  configValue 短信内容
     * @param phone
     * @param params
     * @return
     */
    public boolean messageSendSyn(String configValue,String phone,String[] params){
//        String configValue=sCommentConfigService.getValueByCode(configkey);
        if(StringUtils.isEmpty(configValue)||StringUtils.isEmpty(phone)){
            logger.info(configValue+"configValue为空,请从新配置");
            return false;
        }else {
            if(params!=null&&params.length>0){
                String restr=configValue;
                for(int i=0;i<params.length;i++){
                    if(restr.indexOf("["+i+"]")!=-1){
                        restr=restr.replace("["+i+"]",params[i]);
                    }
                }
                logger.info(phone+"=====>发送消息内容:" + restr);
                System.out.println(phone+"=====>发送消息内容:" + restr);
                boolean result = smsUtil.sendSms(phone,restr);
                logger.info(phone+"=====>发送消息result:" + result);
                System.out.println(phone+"=====>发送消息result:" + result);
                return result;
            }
        }
        return false;
    }
}
